package cn.glc.daily.life.web.controller.manageapi;

import cn.glc.daily.life.common.constans.Developers;
import cn.glc.daily.life.common.utils.JsonResult;
import cn.glc.daily.life.model.dto.user.task.UserTaskAddDto;
import cn.glc.daily.life.model.dto.user.task.UserTaskQueryDto;
import cn.glc.daily.life.model.dto.user.task.UserTaskUpdateDto;
import cn.glc.daily.life.model.dto.user.task.UserTaskUpdateStatusDto;
import cn.glc.daily.life.model.vo.user.task.UserTaskDetailVo;
import cn.glc.daily.life.web.service.user.UserTaskService;
import cn.simon.free.logtrack.annotation.LogTrack;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 用户任务
 *
 * @author simon
 * @date 2021/5/11 17:46
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@RestController
@RequestMapping(value = "/userTask")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class UserTaskManageApiController {

    private final UserTaskService userTaskService;

    /**
     * 用户任务分页查询
     *
     * @param userTaskQueryDto
     * @return
     */
    @PostMapping(value = "/listPage")
    @ApiOperation(value = "用户任务分页查询", notes = Developers.CY)
    public JsonResult listPage(@Valid @RequestBody UserTaskQueryDto userTaskQueryDto) {
        try {
            return JsonResult.of(userTaskService.listPage(userTaskQueryDto));
        } catch (Exception e) {
            log.error("用户任务分页查询|参数:{}|异常", JSON.toJSONString(userTaskQueryDto), e);
            return JsonResult.of(e);
        }
    }

    /**
     * 用户添加任务
     *
     * @param userTaskAddDto
     * @return
     */
    @PostMapping(value = "/addTask")
    @ApiOperation(value = "用户添加任务", notes = Developers.CY)
    @LogTrack(isOut = true)
    public JsonResult addTask(@Valid @RequestBody UserTaskAddDto userTaskAddDto) {
        try {
            return JsonResult.of(userTaskService.addTask(userTaskAddDto));
        } catch (Exception e) {
            log.error("用户添加任务|参数:{}|异常", JSON.toJSONString(userTaskAddDto), e);
            return JsonResult.of(e);
        }
    }

    /**
     * 用户删除任务
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "用户删除任务", notes = Developers.CY)
    @PostMapping(value = "/deleteTask")
    public JsonResult deleteTask(@NotNull(message = "标识不为空") @Valid Long id) {
        try {
            return JsonResult.returnSuccess(userTaskService.deleteTask(id));
        } catch (Exception e) {
            log.error("用户添加任务|参数:{}|异常", id, e);
            return JsonResult.of(e);
        }
    }

    /**
     * 用户查询任务
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "用户查询任务", notes = Developers.CY)
    @GetMapping(value = "/queryTask")
    public JsonResult<UserTaskDetailVo> queryTask(@NotNull(message = "标识不为空") @Valid Long id) {
        try {
            return JsonResult.returnSuccess(userTaskService.queryTask(id));
        } catch (Exception e) {
            log.error("用户查询任务|参数:{}|异常", id, e);
            return JsonResult.of(e);
        }
    }

    /**
     * 用户修改任务
     *
     * @param userTaskUpdateDto
     * @return
     */
    @ApiOperation(value = "用户修改任务", notes = Developers.CY)
    @PostMapping(value = "/updateTask")
    public JsonResult updateTask(@Valid @RequestBody UserTaskUpdateDto userTaskUpdateDto) {
        try {
            return JsonResult.returnSuccess(userTaskService.updateTask(userTaskUpdateDto));
        } catch (Exception e) {
            log.error("用户添加任务|参数:{}|异常", JSON.toJSONString(userTaskUpdateDto), e);
            return JsonResult.of(e);
        }
    }

    /**
     * 用户修改任务状态
     *
     * @param userTaskUpdateStatusDto
     * @return
     */
    @ApiOperation(value = "用户修改任务状态", notes = Developers.CY)
    @PostMapping(value = "/updateTaskStatus")
    public JsonResult updateTaskStatus(@Valid @RequestBody UserTaskUpdateStatusDto userTaskUpdateStatusDto) {
        try {
            return JsonResult.returnSuccess(userTaskService.updateTaskStatus(userTaskUpdateStatusDto));
        } catch (Exception e) {
            log.error("用户修改任务状态|参数:{}|异常", JSON.toJSONString(userTaskUpdateStatusDto), e);
            return JsonResult.of(e);
        }
    }
}
